PATHMac OS 8 and 9 Developer Documentation > Interapplication Communication > AppleScript for Scripters >

AppleScript Language Guide

   

ID

The ID reference form specifies an object by the value of its ID property. You can use this reference form only for objects that have an ID property.

SYNTAX
className id IDvalue

where

className is the class identifier for the specified object.

IDvalue is the value of the object's ID property.

EXAMPLES
tell application "Finder"
    id of every disk --typical result: {-1, -2, -3}
    id of disk "Hard Disk" --typical result: -2
end tell
NOTES

Although ID properties are most often integers, an ID property can belong to any class. An application that includes ID properties must guarantee that the IDs are unique within a container. Some applications may also provide additional guarantees, such as ensuring the uniqueness of an ID among all objects.

The value of an ID property is not modifiable. It does not change even if the object is moved within the container. This allows you to save an object's ID and use it to refer to the object for as long as the object exists. In some scripts you may wish to refer to an object by its ID, rather than by a property such as its name, which may change. The following two examples demonstrate the advantage of using an ID when creating a new folder and renaming it.

The next script generates an error because it gets a reference to a newly created folder, renames the folder, then tries to use the reference (which still refers to the original name) to open the folder:

tell application "Finder"
    set newFolder to make new folder at startup disk
        --result: folder "untitled folder" of startup disk
        --          of application "Finder"
    set name of newFolder to "New Folder Name"
    open newFolder
        --result: ERROR, because the folder has been renamed but the
        --  reference still refers to the original "untitled folder"
end tell

The following script successfully creates a new folder, renames it, and opens it because it uses a folder ID, which doesn't change when you change the folder's name:

tell application "Finder"
    set newFolder to make new folder at startup disk
        --result: folder "untitled folder" of startup disk
        --          of application "Finder"
    set newFolderID to id of newFolder
        --result: an ID, such as 27568
    set name of newFolder to "New Folder Name"
    open folder id newFolderID of startup disk
        --result: Successfully opens new folder named "New Folder Name"
        --          using the folder ID, which is unique and permanent.
end tell

Instead of using a name, you could keep track of a file or folder by its index, using statements such as open file 1 or open the first folder. However, because file and folder indexes are ordered alphabetically, they are likely to refer to a different item after renaming.

You can use an ID to keep track of a file as well as a folder, though because the Finder doesn't currently support opening a file by its file ID, you have to use an approach similar to the one shown in the next script:

tell application "Finder"
    set fileID to the id of the first file of disk "Hard Disk"
        --The script executes some command that, either directly or
        -- indirectly, causes file 1 to be renamed as "NewName".
    set fileName to the name of the first file ¬
        of disk "Hard Disk" whose id is fileID
        --result: "NewName"
    open file fileName of disk "Hard Disk" -- Opens original file.
end tell

A still better way to keep track of files and folders is to use an alias reference, as described in References to Files.

Applications are not required to support ID properties. To find out if or how an application uses ID properties, see the documentation for the application.


© 1999 Apple Computer, Inc. – (Last Updated 21 May 99)